<template>
  <div>
    <!-- 面包屑导航区域 -->
    <nav-bread name1="商品管理" name2="商品列表"></nav-bread>

    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" class="input-with-select" v-model="queryInfo.query"
           @keyup.enter.native="seekGoods" clearable @clear="getGoodList">
            <el-button slot="append" icon="el-icon-search" @click="seekGoods" ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 获取到的商品表格 -->
      <el-table :data="goodsList" style="width:100%" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price" width="85px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="85px"></el-table-column>
        <el-table-column label="创建时间" prop="upd_time" width="140px"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoods(scope.row.goods_id)">删除</el-button>
        </template>
        </el-table-column>
      </el-table>

      <!-- 页签区域 -->
          <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total" background>
    </el-pagination>

    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 发送请求参数对象
      queryInfo:{
        query:'',
        pagenum:1,
        pagesize:10
      },
      // 获取到的参数列表
      goodsList:[],
      // 获取到的参数数量
      total:0,
    }
  },
  created(){
     this.getGoodList()
  },
  methods:{
    // 根据分页获取相对应的参数
     async getGoodList(){
       const {data,}=await this.$http.get(`goods`,{
          params:this.queryInfo
        });
        if(data.meta.status!==200)return this.$message.error('获取失败');
        data.data.goods.forEach(item=>{
          item.upd_time=this.$dayjs(item.upd_time).format('YYYY-MM-DD HH:mm')
        });
         this.goodsList=data.data.goods;
         this.total=data.data.total;
         console.log(data.data.goods);
      },

      // 每页显示数据数量发生改变时
      handleSizeChange(size){
          this.queryInfo.pagesize=size;
          this.getGoodList();
      },

      // 切换分页事件
      handleCurrentChange(num){
         this.queryInfo.pagenum=num;
         this.getGoodList();
      },
      
      // 查询数据
      seekGoods(){
        this.getGoodList();
      },

      // 删除商品
     async removeGoods(id){
        const confirmResult=await this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
        ;
        // 用户确认删除
        // 用户确认删除，返回值是confirm
        // 取消返回值是cancel
        if(confirmResult!=='confirm'){
          return this.$message.info('已取消删除')
        }
        // 用户确认删除，发送请求删除该数据
        const {data}=await this.$http.delete(`goods/${id}`)
        if(data.meta.status!==200) return this.$message.error(data.meta.msg);
         this.$message.success('删除成功');
         this.getGoodList();
      },

      // 添加商品
      addGoods(){
        this.$router.push('/goods/add')
      }
  }
};
</script>

<style lang="less" scoped>
 .el-table{
   margin-top: 15px;
 }
</style>>
